package cn.iocoder.yudao.module.infra.repository.mybatis.dao.logger;

import cn.iocoder.yudao.common.pojo.PageResult;
import cn.iocoder.yudao.core.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.core.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.infra.repository.mybatis.domain.logger.ApiAccessLog;
import cn.iocoder.yudao.module.infra.vo.logger.apiaccesslog.ApiAccessLogExportReqVO;
import cn.iocoder.yudao.module.infra.vo.logger.apiaccesslog.ApiAccessLogPageReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * API 访问日志 Mapper
 */
@Mapper
public interface ApiAccessLogMapper extends BaseMapperX<ApiAccessLog> {

    default PageResult<ApiAccessLog> selectPage(ApiAccessLogPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ApiAccessLog>()
                .eqIfPresent(ApiAccessLog::getUserId, reqVO.getUserId())
                .eqIfPresent(ApiAccessLog::getUserType, reqVO.getUserType())
                .eqIfPresent(ApiAccessLog::getApplicationName, reqVO.getApplicationName())
                .likeIfPresent(ApiAccessLog::getRequestUrl, reqVO.getRequestUrl())
                .betweenIfPresent(ApiAccessLog::getBeginTime, reqVO.getBeginTime())
                .geIfPresent(ApiAccessLog::getDuration, reqVO.getDuration())
                .eqIfPresent(ApiAccessLog::getResultCode, reqVO.getResultCode())
                .orderByDesc(ApiAccessLog::getId)
        );
    }

    default List<ApiAccessLog> selectList(ApiAccessLogExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ApiAccessLog>()
                .eqIfPresent(ApiAccessLog::getUserId, reqVO.getUserId())
                .eqIfPresent(ApiAccessLog::getUserType, reqVO.getUserType())
                .eqIfPresent(ApiAccessLog::getApplicationName, reqVO.getApplicationName())
                .likeIfPresent(ApiAccessLog::getRequestUrl, reqVO.getRequestUrl())
                .betweenIfPresent(ApiAccessLog::getBeginTime, reqVO.getBeginTime())
                .geIfPresent(ApiAccessLog::getDuration, reqVO.getDuration())
                .eqIfPresent(ApiAccessLog::getResultCode, reqVO.getResultCode())
                .orderByDesc(ApiAccessLog::getId)
        );
    }

}
